package main.singleton;

/**
 * @Author: SerMs
 * @Date: 2024/06/17 17 09
 * @Email: 1839928782@qq.com
 * @Blog: https://serms.top
 * 懒汉单例 只有在调用静态方法的时候才会创建对象
 * 但是是不安全的，线程不安全
 **/
public class Singleton03 {
    // 懒汉式的单例模式
    private static Singleton03 INSTANCE;

    private Singleton03() {
    }

    public static Singleton03 getInstance() {
        // 这里会带来线程安全问题
        if (INSTANCE == null) {
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            INSTANCE = new Singleton03();
        }
        return INSTANCE;
    }

    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            // 多线程
            new Thread(() -> System.out.println(Singleton03.getInstance().hashCode())).start();
        }
    }

}
